import { GameConfig } from "../configs/GameConfig";
import { ILotteryElement } from "../configs/Lottery";
import { Attribute } from "../etc/Attribute";
import { RandomSection } from "../etc/RandomSection";
import { util } from "../etc/Util";
import { LotteryWindow } from "../ui/LotteryWindow";
import { Notice } from "../ui/Notice";
import { ItemModuleData, ItemModuleS } from "./ItemModule";
import { PlayerModuleS } from "./PlayerModule";

export interface RewardInformation {
    isItem: boolean
    rewardID: number
    rewardCount: number
}

export const LotteryRandomSectionMap: Map<number, RandomSection> = new Map()
const FillLotteryRandomSection = () => {
    for (const cfg of GameConfig.Lottery.getAllElement()) {
        if (!LotteryRandomSectionMap.has(cfg.Type)) {
            LotteryRandomSectionMap.set(cfg.Type, new RandomSection())
        }
        let section = LotteryRandomSectionMap.get(cfg.Type)
        section.pushSection(cfg.RandomFactor, cfg.ID)
    }
}
FillLotteryRandomSection()

export namespace LotteryUtil {
    let lotteryMapping: Map<number, ILotteryElement[]>

    export function getLotteryCfgArrayByType(type: number) {
        if (!lotteryMapping) initMapping()
        return lotteryMapping.get(type)
    }

    function initMapping() {
        lotteryMapping = new Map()
        for (const cfg of GameConfig.Lottery.getAllElement()) {
            if (!lotteryMapping.has(cfg.Type)) {
                lotteryMapping.set(cfg.Type, [])
            }
            lotteryMapping.get(cfg.Type).push(cfg)
        }
    }
}

export class LotteryModuleC extends ModuleC<LotteryModuleS, null> {
    protected onStart(): void {
        for (const cfg of GameConfig.LotteryEntry.getAllElement()) {
            let trigger = <Trigger>GameObject.findGameObjectById(cfg.TriggerGUID)
            trigger.onEnter.add(this.onLotteryTriggerEnter.bind(this, cfg.ID))
        }
    }

    private onLotteryTriggerEnter(entryCfgID: number, other: GameObject) {
        if (!util.isSelfPlayer(other)) return
        let window = UIService.show(LotteryWindow)
        window.setInfo(entryCfgID)
    }

    send_draw_request(entryCfgID: number) {
        this.server.net_draw_request(entryCfgID)
    }

    net_draw_res(info: RewardInformation) {
        let name: string
        if(!info.isItem){
            name = GameConfig.Attribute.getElement(info.rewardID).Name
            Notice.showTopNotice(`获得'${name}'*${info.rewardCount}`)
            return
        }
        let itemCfg = GameConfig.Item.getElement(info.rewardID)
        if(itemCfg.IconIsAssetsIcon){
            Notice.showSecondWithAssetsIcon(itemCfg.Icon, itemCfg.Name)
        }else{
            Notice.showSecondWithIcon(itemCfg.Icon, itemCfg.Name)
        }
    }
}

export class LotteryModuleS extends ModuleS<LotteryModuleC, null> {

    net_draw_request(entryCfgID: number) {
        let cfg = GameConfig.LotteryEntry.getElement(entryCfgID)

        let itemMD = ModuleService.getModule(ItemModuleS)
        let playerMD = ModuleService.getModule(PlayerModuleS)
        // 金币不足
        if(playerMD.getPlayerAttr(this.currentPlayerId, Attribute.EnumAttributeType.money) < cfg.CostMoney) return

        let randomRewardCfgID = LotteryRandomSectionMap.get(cfg.Type).getRandom()
        let rewardCfg = GameConfig.Lottery.getElement(randomRewardCfgID)

        if (rewardCfg.IsItem) {
            itemMD.addItems(this.currentPlayerId, [
                {
                    cfgID: rewardCfg.RewardID,
                    count: rewardCfg.RewardCount
                }
            ])
        }
        else {
            playerMD.addPlayerAttr(this.currentPlayerId, rewardCfg.RewardID, rewardCfg.RewardCount)
        }

        playerMD.reducePlayerAttr(this.currentPlayerId, Attribute.EnumAttributeType.money, cfg.CostMoney)

        this.getClient(this.currentPlayerId).net_draw_res({
            isItem: rewardCfg.IsItem,
            rewardID: rewardCfg.RewardID,
            rewardCount: rewardCfg.RewardCount,
        })
    }
}